package com.product.magina.wechatserver.controller;

import com.google.common.base.Preconditions;
import com.magina.common.exception.BusinessException;
import com.magina.common.response.Response;
import com.product.magina.wechatserver.dao.DsmtPublicWechatDao;
import com.product.magina.wechatserver.service.DsmtSMSConnectiontService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@RestController
@RequestMapping(value = "SMSConnection")
@Slf4j
@Api(value = "/SMSConnection", description = "发送短信接口")
public class DsmtSMSController {

    @Autowired
    private DsmtSMSConnectiontService dsmtSMSConnectiontService;
    @Autowired
    private DsmtPublicWechatDao dsmtPublicWechatDao;



    @ApiOperation(value = "获取验证码",notes = "{\"openId\":\"asdasd1\",\n" +
            "\"phone\":\"18556720261\"}")
    @PostMapping(value = "/getSMSVerificationCode")
    public Response getSMSVerificationCode(@RequestBody Map map) throws BusinessException {
        Preconditions.checkArgument(!StringUtils.isEmpty(map.get("openId").toString()),"微信用户的openId不能为空");
        Preconditions.checkArgument(!StringUtils.isEmpty(map.get("phone").toString()),"微信用户的phone不能为空");
        Object result = dsmtSMSConnectiontService.getSMSVerificationCode(map.get("openId").toString(),map.get("phone").toString());
        return Response.succeed(result);
    }

    @ApiOperation(value = "输入验证码登录",notes = "{\"openId\":\"asdasd1\", \"verificationCode\":\"142663\", \"phone\":\"18556720261\"}")
    @PostMapping(value = "/loginByVerificationCode")
    public Response loginByVerificationCode(@RequestBody Map map) throws BusinessException {
        Preconditions.checkArgument(!StringUtils.isEmpty(map.get("openId").toString()),"微信用户的openId不能为空");
        Preconditions.checkArgument(!StringUtils.isEmpty(map.get("verificationCode").toString()),"微信用户的verificationCode不能为空");
        Preconditions.checkArgument(!StringUtils.isEmpty(map.get("phone").toString()),"微信用户的phone不能为空");
        Map result = dsmtSMSConnectiontService.loginByVerificationCode(map.get("openId").toString(),map.get("verificationCode").toString(),map.get("phone").toString());
        return Response.succeed(result);
    }
}
